package o;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

public class fxd extends fxv {
    @Nullable
    static fxd ˊ;
    private static final long ˋ = TimeUnit.MILLISECONDS.toNanos(ˎ);
    private static final long ˎ = TimeUnit.SECONDS.toMillis(60);
    @Nullable
    private fxd ʻ;
    private boolean ˏ;
    private long ॱॱ;

    static final class b extends Thread {
        b() {
            super("Okio Watchdog");
            setDaemon(true);
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void run() {
            /*
            r3 = this;
        L_0x0000:
            r1 = o.fxd.class;
            monitor-enter(r1);	 Catch:{ InterruptedException -> 0x000e }
            r0 = o.fxd.ˎ();	 Catch:{ all -> 0x000b }
            if (r0 != 0) goto L_0x0010;
        L_0x0009:
            monitor-exit(r1);	 Catch:{ all -> 0x000b }
            goto L_0x0000;
        L_0x000b:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x000b }
            throw r0;	 Catch:{ InterruptedException -> 0x000e }
        L_0x000e:
            r0 = move-exception;
            goto L_0x0000;
        L_0x0010:
            r2 = o.fxd.ˊ;	 Catch:{ all -> 0x000b }
            if (r0 != r2) goto L_0x0019;
        L_0x0014:
            r0 = 0;
            o.fxd.ˊ = r0;	 Catch:{ all -> 0x000b }
            monitor-exit(r1);	 Catch:{ all -> 0x000b }
            return;
        L_0x0019:
            monitor-exit(r1);	 Catch:{ all -> 0x000b }
            r0.ॱ();	 Catch:{ InterruptedException -> 0x000e }
            goto L_0x0000;
            */
            throw new UnsupportedOperationException("Method not decompiled: o.fxd.b.run():void");
        }
    }

    public final void ar_() {
        if (this.ˏ) {
            throw new IllegalStateException("Unbalanced enter/exit");
        }
        long as_ = as_();
        boolean at_ = at_();
        if (as_ != 0 || at_) {
            this.ˏ = true;
            ˋ(this, as_, at_);
        }
    }

    private static synchronized void ˋ(fxd o_fxd, long j, boolean z) {
        synchronized (fxd.class) {
            if (ˊ == null) {
                ˊ = new fxd();
                new b().start();
            }
            long nanoTime = System.nanoTime();
            if (j != 0 && z) {
                o_fxd.ॱॱ = Math.min(j, o_fxd.ˏ() - nanoTime) + nanoTime;
            } else if (j != 0) {
                o_fxd.ॱॱ = nanoTime + j;
            } else if (z) {
                o_fxd.ॱॱ = o_fxd.ˏ();
            } else {
                throw new AssertionError();
            }
            long ˊ = o_fxd.ˊ(nanoTime);
            fxd o_fxd2 = ˊ;
            while (o_fxd2.ʻ != null && ˊ >= o_fxd2.ʻ.ˊ(nanoTime)) {
                o_fxd2 = o_fxd2.ʻ;
            }
            o_fxd.ʻ = o_fxd2.ʻ;
            o_fxd2.ʻ = o_fxd;
            if (o_fxd2 == ˊ) {
                fxd.class.notify();
            }
        }
    }

    public final boolean ˋ() {
        if (!this.ˏ) {
            return false;
        }
        this.ˏ = false;
        return ˋ(this);
    }

    private static synchronized boolean ˋ(fxd o_fxd) {
        boolean z;
        synchronized (fxd.class) {
            for (fxd o_fxd2 = ˊ; o_fxd2 != null; o_fxd2 = o_fxd2.ʻ) {
                if (o_fxd2.ʻ == o_fxd) {
                    o_fxd2.ʻ = o_fxd.ʻ;
                    o_fxd.ʻ = null;
                    z = false;
                    break;
                }
            }
            z = true;
        }
        return z;
    }

    private long ˊ(long j) {
        return this.ॱॱ - j;
    }

    protected void ॱ() {
    }

    public final fxx ˎ(final fxx o_fxx) {
        return new fxx(this) {
            final /* synthetic */ fxd ˋ;

            public void ˎ(fxi o_fxi, long j) throws IOException {
                fxy.ˊ(o_fxi.ˎ, 0, j);
                long j2 = j;
                while (j2 > 0) {
                    fxt o_fxt = o_fxi.ॱ;
                    long j3 = 0;
                    while (j3 < 65536) {
                        long j4 = ((long) (o_fxt.ˋ - o_fxt.ˎ)) + j3;
                        if (j4 >= j2) {
                            j3 = j2;
                            break;
                        } else {
                            o_fxt = o_fxt.ʼ;
                            j3 = j4;
                        }
                    }
                    this.ˋ.ar_();
                    try {
                        o_fxx.ˎ(o_fxi, j3);
                        j2 -= j3;
                        this.ˋ.ˊ(true);
                    } catch (IOException e) {
                        throw this.ˋ.ˊ(e);
                    } catch (Throwable th) {
                        this.ˋ.ˊ(false);
                    }
                }
            }

            public void flush() throws IOException {
                this.ˋ.ar_();
                try {
                    o_fxx.flush();
                    this.ˋ.ˊ(true);
                } catch (IOException e) {
                    throw this.ˋ.ˊ(e);
                } catch (Throwable th) {
                    this.ˋ.ˊ(false);
                }
            }

            public void close() throws IOException {
                this.ˋ.ar_();
                try {
                    o_fxx.close();
                    this.ˋ.ˊ(true);
                } catch (IOException e) {
                    throw this.ˋ.ˊ(e);
                } catch (Throwable th) {
                    this.ˋ.ˊ(false);
                }
            }

            public fxv ˊ() {
                return this.ˋ;
            }

            public String toString() {
                return "AsyncTimeout.sink(" + o_fxx + ")";
            }
        };
    }

    public final fxw ˎ(final fxw o_fxw) {
        return new fxw(this) {
            final /* synthetic */ fxd ˊ;

            public long ˊ(fxi o_fxi, long j) throws IOException {
                this.ˊ.ar_();
                try {
                    long ˊ = o_fxw.ˊ(o_fxi, j);
                    this.ˊ.ˊ(true);
                    return ˊ;
                } catch (IOException e) {
                    throw this.ˊ.ˊ(e);
                } catch (Throwable th) {
                    this.ˊ.ˊ(false);
                }
            }

            public void close() throws IOException {
                try {
                    o_fxw.close();
                    this.ˊ.ˊ(true);
                } catch (IOException e) {
                    throw this.ˊ.ˊ(e);
                } catch (Throwable th) {
                    this.ˊ.ˊ(false);
                }
            }

            public fxv ˊ() {
                return this.ˊ;
            }

            public String toString() {
                return "AsyncTimeout.source(" + o_fxw + ")";
            }
        };
    }

    final void ˊ(boolean z) throws IOException {
        if (ˋ() && z) {
            throw ˋ(null);
        }
    }

    final IOException ˊ(IOException iOException) throws IOException {
        return !ˋ() ? iOException : ˋ(iOException);
    }

    protected IOException ˋ(@Nullable IOException iOException) {
        IOException interruptedIOException = new InterruptedIOException("timeout");
        if (iOException != null) {
            interruptedIOException.initCause(iOException);
        }
        return interruptedIOException;
    }

    @Nullable
    static fxd ˎ() throws InterruptedException {
        fxd o_fxd = ˊ.ʻ;
        if (o_fxd == null) {
            long nanoTime = System.nanoTime();
            fxd.class.wait(ˎ);
            if (ˊ.ʻ != null || System.nanoTime() - nanoTime < ˋ) {
                return null;
            }
            return ˊ;
        }
        nanoTime = o_fxd.ˊ(System.nanoTime());
        if (nanoTime > 0) {
            long j = nanoTime / 1000000;
            fxd.class.wait(j, (int) (nanoTime - (1000000 * j)));
            return null;
        }
        ˊ.ʻ = o_fxd.ʻ;
        o_fxd.ʻ = null;
        return o_fxd;
    }
}
